-- metatable 测试


-- __index
-- function func()
--     return "Lua"
-- end

-- other = {
--     foo = func
-- }
-- t = setmetatable({}, {__index = other})
-- print(t.foo())

-- mytable =
--     setmetatable(
--     {key1 = "value1"},
--     {
--         __index = function(mytable, key)
--             if key == "key2" then
--                 return "metatablevalue"
--             else
--                 return nil
--             end
--         end
--     }
-- )

-- print(mytable.key1, mytable.key2, mytable.key3)

-- __newindex 原方法用来对表更新，__index用来对表的访问

mymetatable = {}
mytable = setmetatable({Key1 = "value1"}, {__newindex = mymetatable})
print(mytable.Key1, mymetatable.key1)

mytable.newKey = "value2"
print(mytable.newKey, mymetatable.newKey)

mytable.Key1 = "value2"
print(mytable.Key1, mymetatable.key1)
